package com.fowo.api.sys.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fowo.api.common.model.RException;
import com.fowo.api.sys.entity.SysImportTemplate;
import com.fowo.api.sys.mapper.SysImportTemplateMapper;
import com.fowo.api.sys.model.SysImportTemplateSearchParamPo;
import com.fowo.api.sys.model.SysImportTemplateVo;
import com.fowo.api.sys.service.SysImportTemplateService;
import com.fowo.api.user.model.JwtUserInfo;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Objects;

/**
 * 系统导入模版服务实现
 * @author yl_ls
 */
@Service
public class SysImportTemplateServiceImpl extends ServiceImpl<SysImportTemplateMapper, SysImportTemplate> implements SysImportTemplateService {

    @Override
    public Page<SysImportTemplateVo> searchPage(SysImportTemplateSearchParamPo search) {
        search.setDisableDataPremissions(true);
        search.initSort("t.title", "t.");
        final Page<SysImportTemplateVo> sysImportTemplateVoPage = baseMapper.searchPage(search.toPage(), search);
        if (!sysImportTemplateVoPage.getRecords().isEmpty()) {
            // 填充记录是否当前用户所有
            final JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
            if (jwtUserInfo != null) {
                for (SysImportTemplateVo record : sysImportTemplateVoPage.getRecords()) {
                    record.setMyTemplate(Objects.equals(jwtUserInfo.getUserId(), record.getCreateUser()));
                }
            }
        }
        return sysImportTemplateVoPage;
    }

    @Override
    public Long create(SysImportTemplate sysImportTemplate) {
        final JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
        if (jwtUserInfo != null) {
            sysImportTemplate.setCreateUser(jwtUserInfo.getUserId());
        }
        sysImportTemplate.setCreateTime(new Date());
        validateUnique(sysImportTemplate);
        if(save(sysImportTemplate)) {
            return sysImportTemplate.getId();
        }
        throw new RException("保存数据失败");
    }

    @Override
    public boolean update(SysImportTemplate sysImportTemplate) {
        final JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
        if (jwtUserInfo != null) {
            sysImportTemplate.setLastUpdateUser(jwtUserInfo.getUserId());
        }
        sysImportTemplate.setLastUpdateTime(new Date());
        validateUnique(sysImportTemplate);
        return updateById(sysImportTemplate);
    }

    @Override
    public void delete(Long id) {
        final SysImportTemplate sysImportTemplate = getById(id);
        if (Boolean.TRUE.equals(sysImportTemplate.isProtected())) {
            throw new RException("系统保护内容不能删除");
        }
        removeById(id);
    }

    private void validateUnique(SysImportTemplate sysImportTemplate) {
        if(baseMapper.selectCount(Wrappers.<SysImportTemplate>query()
                .ne(sysImportTemplate.getId() != null, "id", sysImportTemplate.getId())
                .eq("object_name", sysImportTemplate.getObjectName())
                .eq("title", sysImportTemplate.getTitle())
        )>0) {
            throw new RException("相同的标题已经存在！");
        }
    }
}
